home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / testdisk / src / ntfs_io.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-01-09  |  4.5 KB  |  163 lines

  1. /*
  2.  * ntfs_io.c - Unix style disk io functions. Part of the TestDisk project.
  3.  *
  4.  * Original version comes from the Linux-NTFS project.
  5.  * Copyright (c) 2000-2003 Anton Altaparmakov 
  6.  * Copyright (c) 2004 Christophe Grenier
  7.  *
  8.  * This program/include file is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as published
  10.  * by the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program/include file is distributed in the hope that it will be
  14.  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15.  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program (in the main directory of the Linux-NTFS
  20.  * distribution in the file COPYING); if not, write to the Free Software
  21.  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  */
  23. #ifdef HAVE_NTFSPROGS
  24.  
  25. #include "config.h"
  26.  
  27. #include <unistd.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <stdio.h>
  32. #include <sys/stat.h>
  33. #include <fcntl.h>
  34. #include <sys/ioctl.h>
  35. #ifdef HAVE_LINUX_FD_H
  36. #    include <linux/fd.h>
  37. #endif
  38.  
  39. #include "mst.h"
  40. #include "debug.h"
  41. #include "device.h"
  42. #include "types.h"
  43. #include "common.h"
  44.  
  45. #if defined(linux) && defined(_IO) && !defined(BLKGETSIZE)
  46. #    define BLKGETSIZE _IO(0x12,96) /* Get device size in 512byte blocks. */
  47. #endif
  48. /*extern s64 ntfs_pread(struct ntfs_device *dev, const s64 pos, s64 count, void *b); */
  49. /*extern s64 ntfs_pwrite(struct ntfs_device *dev, const s64 pos, s64 count, const void *b); */
  50.  
  51. static int ntfs_device_testdisk_io_open(struct ntfs_device *dev, int flags)
  52. {
  53.     if (NDevOpen(dev)) {
  54.         errno = EBUSY;
  55.         return -1;
  56.     }
  57.     /* Setup our read-only flag. */
  58.     if ((flags & O_RDWR) != O_RDWR)
  59.         NDevSetReadOnly(dev);
  60.     /* Set our open flag. */
  61.     NDevSetOpen(dev);
  62.     return 0;
  63. }
  64.  
  65. static int ntfs_device_testdisk_io_close(struct ntfs_device *dev)
  66. {
  67.     if (!NDevOpen(dev)) {
  68.         errno = EBADF;
  69.         return -1;
  70.     }
  71.     NDevClearOpen(dev);
  72.     return 0;
  73. }
  74.  
  75. static s64 ntfs_device_testdisk_io_seek(struct ntfs_device *dev, s64 offset,
  76.         int whence)
  77. {
  78.   t_my_data *my_data=(t_my_data*)dev->d_private;
  79.   switch(whence)
  80.   {
  81.     case SEEK_SET:
  82.       my_data->offset=offset;
  83.       break;
  84.     case SEEK_CUR:
  85.       my_data->offset+=offset;
  86.       break;
  87.     case SEEK_END:
  88.       my_data->offset=my_data->partition->part_size+offset;
  89.       break;
  90.   }
  91.   return my_data->offset;
  92. }
  93.  
  94. static s64 ntfs_device_testdisk_io_read(struct ntfs_device *dev, void *buf,
  95.         s64 count)
  96. {
  97.   t_my_data *my_data=(t_my_data*)dev->d_private;
  98. /*  ecrit_rapport("offset=%llx count=%lld",my_data->offset,count); */
  99.   if(my_data->disk_car->read(my_data->disk_car,count/SECTOR_SIZE,buf,my_data->partition->lba+my_data->offset/SECTOR_SIZE))
  100.     return 0;
  101.   my_data->offset+=count;
  102.   return count;
  103. }
  104.  
  105. static s64 ntfs_device_testdisk_io_write(struct ntfs_device *dev, const void *buf,
  106.         s64 count)
  107. {
  108.     fprintf(stderr, "ntfs_device_testdisk_io_write() unimplemented\n");
  109.     errno = ENOTSUP;
  110.     return -1;
  111. }
  112.  
  113. static s64 ntfs_device_testdisk_io_pread(struct ntfs_device *dev, void *buf,
  114.         s64 count, s64 offset)
  115. {
  116.     return ntfs_pread(dev, offset, count, buf);
  117. }
  118.  
  119. static s64 ntfs_device_testdisk_io_pwrite(struct ntfs_device *dev, const void *buf,
  120.         s64 count, s64 offset)
  121. {
  122.     return ntfs_pwrite(dev, offset, count, buf);
  123. }
  124.  
  125. static int ntfs_device_testdisk_io_sync(struct ntfs_device *dev)
  126. {
  127.     fprintf(stderr, "ntfs_device_testdisk_io_sync() unimplemented\n");
  128.     errno = ENOTSUP;
  129.     return -1;
  130. }
  131.  
  132. static int ntfs_device_testdisk_io_stat(struct ntfs_device *dev, struct stat *buf)
  133. {
  134.     fprintf(stderr, "ntfs_device_testdisk_io_stat() unimplemented\n");
  135.     errno = ENOTSUP;
  136.     return -1;
  137. }
  138.  
  139. static int ntfs_device_testdisk_io_ioctl(struct ntfs_device *dev, int request,
  140.         void *argp)
  141. {
  142.     fprintf(stderr, "ntfs_device_testdisk_io_ioctl() unimplemented\n");
  143.     errno = ENOTSUP;
  144.     return -1;
  145. }
  146.  
  147. /**
  148.  * Device operations for working with unix style devices and files.
  149.  */
  150. struct ntfs_device_operations ntfs_device_testdisk_io_ops = {
  151.     .open        = ntfs_device_testdisk_io_open,
  152.     .close        = ntfs_device_testdisk_io_close,
  153.     .seek        = ntfs_device_testdisk_io_seek,
  154.     .read        = ntfs_device_testdisk_io_read,
  155.     .write        = ntfs_device_testdisk_io_write,
  156.     .pread        = ntfs_device_testdisk_io_pread,
  157.     .pwrite        = ntfs_device_testdisk_io_pwrite,
  158.     .sync        = ntfs_device_testdisk_io_sync,
  159.     .stat        = ntfs_device_testdisk_io_stat,
  160.     .ioctl        = ntfs_device_testdisk_io_ioctl,
  161. };
  162. #endif
  163.